/*
 *
 *
 */

#include "TitleState.h"
#ifndef _TITLESTATE_CPP_
#define _TITLESTATE_CPP_

/*
 *
 */
void TitleState::load(void)
{
	inputManager = Singleton<InputManager>::getInstance();
	#ifdef _TRACKING_
	    trackManager = Singleton<TrackManager>::getInstance();
	    body = trackManager->getBody();
	#endif
    background = new Sprite("../Images/yellow_background.png",0,0);
    title = new Text("GetSmileys","../Fonts/liberation_sans.ttf",150,150,TTF_STYLE_BOLD,0,0,0,64);
    message = new Text("Press <enter> to continue...","../Fonts/liberation_sans.ttf",390,450,TTF_STYLE_NORMAL,0,0,0,20);
    smile = new Animation("../Images/smile_yellow.png",260.0,220.0,500,4,1);
    leftHand = new Sprite("../Images/leftHand.png",0,0);
    rightHand = new Sprite("../Images/rightHand.png",0,0);
    music = new Audio("../Sounds/01-menu.mp3",1);
    coinSfx = new Audio("../Sounds/coin.wav",0);
    music->play(-1);
    coinSfx->play(0);
}

/*
 *
 */
int TitleState::update(int tDelta)
{
    inputManager->poolEvents(); /*Este método deve ser chamado no início do update de cada estado que utiliza a classe InputManager*/
    smile->update(tDelta);
    
    /*atualiza as posições das mãos*/
    #ifdef _TRACKING_
	    leftHand->updatePos(body->left_centroid.x,body->left_centroid.y);
	    rightHand->updatePos(body->right_centroid.x,body->right_centroid.y);
    #endif
    
    if(inputManager->isEventQuit())
        return -1;
    if(inputManager->isKeyDown(SDLK_RETURN))
        return 2;
    #ifdef _TRACKING_
	    if(inputManager->isKeyDown(SDLK_1))
	        trackManager->setSegmantation(0);
	    if(inputManager->isKeyDown(SDLK_2))
	        trackManager->setSegmantation(1);
	    if(inputManager->isKeyDown(SDLK_3))
	        trackManager->setSegmantation(2);
	    if(inputManager->isKeyDown(SDLK_4))
	        trackManager->setSegmantation(3);
	#endif
    
    return 0;
}

/*
 *
 */
void TitleState::draw(SDL_Surface *screen)
{
    background->render(screen);
    title->render(screen);
    
    /*desenha as mãos*/
    leftHand->render(screen);
    rightHand->render(screen);
    
    message->render(screen);
    smile->render(screen);
}

/*
 *
 */
void TitleState::unload(void)
{
    if(inputManager != NULL)
    {
        Singleton<InputManager>::releaseInstance();
    }
    #ifdef _TRACKING_
	    if(trackManager != NULL)
	    {
	        Singleton<TrackManager>::releaseInstance();
	    }
	#endif
    
    if(coinSfx != NULL)
    {
        delete coinSfx;
        coinSfx = NULL;
    }
    if(music != NULL)
    {
        delete music;
        music = NULL;
    }
    if(message != NULL)
    {
        delete message;
        message = NULL;
    }
    if(rightHand != NULL)
    {
        delete rightHand;
        rightHand  = NULL;
    }
    if(leftHand != NULL)
    {
        delete leftHand;
        leftHand  = NULL;
    }
    if(smile != NULL)
    {
        delete smile;
        smile = NULL;
    }
    if(title != NULL)
    {
        delete title;
        title = NULL;
    }
    if(background != NULL)
    {
        delete background;
        background = NULL;
    }
}

#endif
